#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QProgressDialog>
#include <QElapsedTimer>
#include <QInputDialog>
#include <QMessageBox>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_btnOpen_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, "打开文件", QCoreApplication::applicationDirPath(), "文本文件(*.txt);; 图片文件(*.jpg *.gif *.png);; 所有文件(*.*)");
    if(!fileName.isEmpty())
    {
        ui->plainTextEdit->appendPlainText("打开单个文件成功");
        ui->plainTextEdit->appendPlainText(fileName);
    }
}

void Dialog::on_btnOpenMulti_clicked()
{
    QStringList fileList = QFileDialog::getOpenFileNames(this, "打开文件", QCoreApplication::applicationDirPath(), "文本文件(*.txt);; 图片文件(*.jpg *.gif *.png);; 所有文件(*.*)");
    if(!fileList.isEmpty())
    {
        ui->plainTextEdit->appendPlainText("打开多个文件成功");
        for(const auto &e : fileList)
        {
            ui->plainTextEdit->appendPlainText(e);
        }
    }
}

void Dialog::on_btnSelDir_clicked()
{
    QString dirName = QFileDialog::getExistingDirectory(this, "选择文件夹", QDir::currentPath());
    if(!dirName.isEmpty())
    {
        ui->plainTextEdit->appendPlainText("选择文件夹");
        ui->plainTextEdit->appendPlainText(dirName);
    }
}

void Dialog::on_btnSave_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, "保存文件", QDir::currentPath(),  "文本文件(*.txt);; 图片文件(*.jpg *.gif *.png);; 所有文件(*.*)");
    if(!fileName.isEmpty())
    {
        ui->plainTextEdit->appendPlainText("保存文件");
        ui->plainTextEdit->appendPlainText(fileName);
    }
}

void Dialog::on_btnColor_clicked()
{
    QPalette palette = ui->plainTextEdit->palette();
    QColor color = palette.color(QPalette::Text);
    color = QColorDialog::getColor(color, this);
    palette.setColor(QPalette::Text, color);
    ui->plainTextEdit->setPalette(palette);
}

void Dialog::on_btnFont_clicked()
{
    bool ret = false;
    QFont font = ui->plainTextEdit->font();
    font = QFontDialog::getFont(&ret, this);
    if(ret)
    {
        ui->plainTextEdit->setFont(font);
    }
}

void Dialog::on_btnProgress_clicked()
{
    QString labText="正在复制文件...";
    QString btnText="取消";
    int   minV=0, maxV=200;

    QProgressDialog dlgProgress(labText,btnText, minV, maxV, this);
    // dlgProgress.setWindowModality(Qt::WindowModal);
    connect(&dlgProgress, SIGNAL(canceled()),
            this,SLOT(do_progress_canceled()));

    dlgProgress.setWindowTitle("复制文件");
    dlgProgress.setWindowModality(Qt::WindowModal); //模式对话框
    dlgProgress.setAutoReset(true); //value()达到最大值时自动调用reset()
    dlgProgress.setAutoClose(true); //调用reset()时隐藏窗口

    //   dlgProgress.show();
    QElapsedTimer msCounter;    //计时器
    for(int i=minV; i<=maxV;i++)    //用循环表示操作进度
    {
        dlgProgress.setValue(i);
        dlgProgress.setLabelText(QString::asprintf("正在复制文件,第 %d 个",i));

        msCounter.start();
        while(true)
        {
            if (msCounter.elapsed()>30)   //执行时间30ms
                break;
        }

        //      qApp->processEvents();//无需这一句
        if (dlgProgress.wasCanceled())   //中途取消
            break;
    }
}

void Dialog::do_progress_canceled()
{
    ui->plainTextEdit->appendPlainText("进度条弹窗退出");
}

void Dialog::on_btnInputInt_clicked()
{
    bool ok = false;
    int ret = QInputDialog::getInt(this, "获取整数", "请输入整数", 0, -100, 100, 1,  &ok);
    if(ok)
    {
        ui->plainTextEdit->appendPlainText(QString::asprintf("获取整数：%d", ret));
    }
}

void Dialog::on_btnInputString_clicked()
{
    bool ok = false;
    QString str =  QInputDialog::getText(this, "获取字符串", "请输入字符串", QLineEdit::Normal, "输入字符串", &ok);
    if(ok)
    {
        ui->plainTextEdit->appendPlainText(str);
    }
}

void Dialog::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}

void Dialog::on_btnClose_clicked()
{
    this->close();
}

void Dialog::on_btnInputFloat_clicked()
{
    bool ok = false;
    double ret = QInputDialog::getDouble(this, "获取浮点数", "请输入浮点数",0, 0, 100, 1, &ok);
    if(ok)
    {
        ui->plainTextEdit->appendPlainText(QString::asprintf("获取整数：%f", ret));
    }
}

void Dialog::on_btnInputItem_clicked()
{
    bool ok = false;
    QStringList strList = {"111", "222", "333"};
    QString ret = QInputDialog::getItem(this, "条目选择", "请选择一个条目", strList, 0, true, &ok);
    if(ok)
    {
        ui->plainTextEdit->appendPlainText("选择条目: " + ret);
    }
}


void Dialog::on_btnMsgQuestion_clicked()
{
        QString dlgTitle="Question消息框";
        QString strInfo="文件已被修改，是否保存修改？";

        QMessageBox::StandardButton  defaultBtn=QMessageBox::NoButton; //缺省按钮

        QMessageBox::StandardButton result;//返回选择的按钮
        result=QMessageBox::question(this, dlgTitle, strInfo,
                                       QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                                       defaultBtn);

        if (result==QMessageBox::Yes)
            ui->plainTextEdit->appendPlainText("Question消息框: Yes 被选择");
        else if(result==QMessageBox::No)
            ui->plainTextEdit->appendPlainText("Question消息框: No 被选择");
        else if(result==QMessageBox::Cancel)
            ui->plainTextEdit->appendPlainText("Question消息框: Cancel 被选择");
        else
            ui->plainTextEdit->appendPlainText("Question消息框: 无选择");
}

void Dialog::on_btnMsgInformation_clicked()
{
     QMessageBox::information(this, "提示", "这是一条提示",
                                                               QMessageBox::Yes, QMessageBox::NoButton);
}

void Dialog::on_btnMsgWarning_clicked()
{
    QMessageBox::warning(this, "警告", "这是一条警告", QMessageBox::Yes, QMessageBox::NoButton);
}

void Dialog::on_btnMsgCritical_clicked()
{
    QMessageBox::critical(this, "严重警告", "这是一条严重警告", QMessageBox::Yes, QMessageBox::NoButton);
}


void Dialog::on_btnMsgAbout_clicked()
{
    QMessageBox::about(this, "about", "SEGY文件查看软件 V1.0 \nDesigned by wwb");
}

void Dialog::on_btnMsgAboutQt_clicked()
{
    QMessageBox::aboutQt(this, "aboutQt消息框");
}

